﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;

namespace Stone
{
    public class Module_Radio_Silabs : Module_Radio
    {
        //Variables
        private USBRadio hardwareRadio = new USBRadio();
        private Timer TimerUpdateData = new Timer();

        //Methods
        new public void ON()
        {
            LoadStations();
            hardwareRadio.InitializeRadio();
            hardwareRadio.UnMute();
            hardwareRadio.TuneFreq(CurrentFrequency);            

            this.TimerUpdateData.Tick += new System.EventHandler(this.TimerUpdateData_Tick);
            TimerUpdateData.Interval = 1000;
            TimerUpdateData.Start();

            //hardwareRadio.Set_Volume(127, 127);        
        }
        new public void OFF()
        {
            this.TimerUpdateData.Tick -= new System.EventHandler(this.TimerUpdateData_Tick);
            hardwareRadio.ShutdownRadio();
            //hardwareRadio.UnMute();
            //hardwareRadio.Set_Volume(127, 127);
        }
        new public void SeekUp()
        {
            hardwareRadio.SeekFreq(true);
        }
        new public void SeekDown()
        {
            hardwareRadio.SeekFreq(false);
        }
        new public void TuneFrequencyUp(int step)
        {
            hardwareRadio.TuneFreq(hardwareRadio.GetStation() + step);
        }
        new public void TuneFrequencyDown(int step)
        {
            hardwareRadio.TuneFreq(hardwareRadio.GetStation() - step);
        }
        new public void Tune(int frequency)
        {
            hardwareRadio.TuneFreq(frequency);
        }
        new public void Tune(string memoryNumber)
        {
            int id = Convert.ToInt32(memoryNumber);
            Station stationToLoad = Station_get(id);
            Tune(stationToLoad.frequency);
        }
        new public int SignalStrenght()
        {
            return hardwareRadio.SignalStrenght();
        }
        private void TimerUpdateData_Tick(object sender, EventArgs e)
        {
            RDSname = hardwareRadio.GetRDSname();
            RDSmessage = hardwareRadio.GetRDSInfo();
            CurrentFrequency = hardwareRadio.GetStation();
        }
    }

    public class USBRadio
    {
        //Variables
        private string _frequency = "";
        private DateTime dt = DateTime.Now;
        private static bool init = false;
        private string usbradiodll = Settings.Application_Path + "\\USBRadio.dll";

        //enumerations
        public enum MuteCondition
        {
            Off,
            On
        }

        /*
         ?VB_GetFrequency@@YG_NN@Z
        DontUseInternalAudio
        FMTune
        FMTuneDown
        FMTuneUp
        GetCurrStation
        GetModuleInfo
        GetModuleName
        GetRDS
        GetSignal
        GetVolume
        HWDeInit
        HWInit
        IsStereo
        RegisterRadioText
        RegisterTAStart
        RegisterTAStop
        ScanStation
        SeekStation
        SetExFlags
        SetMute
        SetVolume
        TuneFreq
        VB_GetAFList
        VB_GetEon
        VB_GetEONNumber
        VB_GetModuleName
        VB_GetRadioRegisters
        VB_GetRDS
        VB_GetRDSEON
        VB_GetRDSMS
        VB_GetRDSPI
        VB_GetRDSPICountry
        VB_GetRDSPIRegion
        VB_GetRDSPS
        VB_GetRDSPTY
        VB_GetRDSPTYString
        VB_GetRDSRegisters
        VB_GetRDSTA
        VB_GetRDSText
        VB_GetRDSTP
        VolumeUpDown

         */
        //Dll Imports
        [DllImport("USBRadio.dll")]
        public static extern bool FMTune(int frequency);
        [DllImport("USBRadio.dll")]
        public static extern int GetCurrStation();
        [DllImport("USBRadio.dll")]
        public static extern StringBuilder GetModuleName();
        [DllImport("USBRadio.dll")]
        public static extern byte GetSignal();
        [DllImport("USBRadio.dll")]
        public static extern bool HWDeInit();
        [DllImport("USBRadio.dll")]
        public static extern bool HWInit();
        [DllImport("USBRadio.dll")]
        public static extern bool IsStereo();
        [DllImport("USBRadio.dll")]
        public static extern long SeekStation(bool SeekUp);
        [DllImport("USBRadio.dll")]
        public static extern void SetMute(bool mute);
        [DllImport("USBRadio.dll")]
        public static extern void SetVolume(int left, int right);
        //[DllImport("USBRadio.dll")]
        //public static extern double VB_GetFrequency();
        [DllImport("USBRadio.dll")]
        public static extern bool VB_GetModuleName(StringBuilder sNameBuffer, ref short iNameLength);
        [DllImport("USBRadio.dll")]
        public static extern bool VB_GetRadioRegisters(StringBuilder sRegBuffer, ref short iRegLength);
        [DllImport("USBRadio.dll")]
        public static extern bool VB_GetRDSPS(StringBuilder sNameBuffer, ref short iNameLength);
        [DllImport("USBRadio.dll")]
        public static extern bool VB_GetRDSRegisters(StringBuilder sRegBuffer, ref short iRegLength);
        [DllImport("USBRadio.dll")]
        public static extern bool VB_GetRDSText(StringBuilder sRDSBuffer, ref short iRDSLength);

        //Methods
        public string GetRDSname()
        {
            string result = "";

            try
            {
                short capacity = 8;
                short num2 = 0x100;

                StringBuilder sNameBuffer = new StringBuilder(capacity);
                StringBuilder sRDSBuffer = new StringBuilder(num2);

                if (VB_GetRDSPS(sNameBuffer, ref num2))
                    result += sNameBuffer.ToString();
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - GetRDSInformation() exception = " + exception.ToString());
            }

            if (result.Length > 50)
                result = result.Substring(0, 50);

            string txt = result.Replace("", "").Trim();

            return txt;
        }
        public string GetRDSInfo()
        {
            string result = "";

            try
            {
                short capacity = 1024;
                short num2 = 0x100;

                StringBuilder sNameBuffer = new StringBuilder(capacity);
                StringBuilder sRDSBuffer = new StringBuilder(num2);

                if (VB_GetRDSText(sRDSBuffer, ref num2))
                    result += " " + sRDSBuffer.ToString();
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - GetRDSInformation() exception = " + exception.ToString());
            }

            if (result.Length > 50)
                result = result.Substring(0, 50);

            return result.Trim();
        }
        public int GetStation()
        {
            int num = -1;

            try
            {
                num = GetCurrStation() / 10;
                Int32 st = GetCurrStation();
                this._frequency = (((double)GetCurrStation()) / 1000.0).ToString();
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - GetStation() exception = " + exception.ToString());
            }

            return num;
        }
        public bool InitializeRadio()
        {
            if (!init)
            {
                init = true;

                try
                {
                    init = HWInit();
                }
                catch (Exception exception)
                {
                    Log("USBRadio.DLL - InitializeRadio() exception = " + exception.ToString());
                }
            }

            return init;
        }        
        public void Mute()
        {
            try
            {
                if (init)
                    SetMute(true);
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - Mute() exception = " + exception.ToString());
            }
        }
        public void UnMute()
        {
            try
            {
                if (init)
                    SetMute(false);
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - UnMute() exception = " + exception.ToString());
            }
        }
        public bool ResumeRadio(int currentFreq)
        {
            try
            {
                HWInit();
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - ResumeRadio() exception = " + exception.ToString());
            }
            return true;
        }
        public void SeekFreq(bool searchDirection)
        {
            try
            {
                if (init)
                    SeekStation(searchDirection);
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - SeekFreq() exception = " + exception.ToString());
            }
        }
        public void ShutdownRadio()
        {
            try
            {
                if (init)
                {
                    SetMute(true);
                    HWDeInit();
                }

                init = false;
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - ShutdownRadio() exception = " + exception.ToString());
            }
        }
        public bool Is_Stereo()
        {
            try
            {
                return IsStereo();
            }
            catch (Exception exception)
            {
                Log(exception.ToString());
            }

            return false;
        }
        public int TuneFreq(int Freq)
        {
            try
            {
                if (init)
                {
                    if (Freq > 0x2a30)
                        return 0;

                    if (Freq < 0x21fc)
                        return 0;

                    FMTune(Freq * 10);
                    Freq = Convert.ToInt32((int)(GetCurrStation() / 10));
                }
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - TuneFreq() exception = " + exception.ToString());
            }

            return Freq;
        }
        public int SignalStrenght()
        {
            int result = -1;
            byte signal;

            try
            {
                if (init)
                {
                    signal = GetSignal();
                    result = (int)signal;
                }
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - ShutdownRadio() exception = " + exception.ToString());
            }

            return result;
        }
        public void Set_Volume(int left, int right)
        {
            try
            {
                if (init)
                {
                    SetVolume(left, right);
                }
            }
            catch (Exception exception)
            {
                Log("USBRadio.DLL - TuneFreq() exception = " + exception.ToString());
            }
        }
        private void Log(string text)
        {
            File.AppendAllText(Settings.LOG_Path, DateTime.Now.ToString() + " " + text + "\n\r\n\r");
        }
    }
}


